package com.struggle.rabbitmqconsumer.config;

import com.struggle.common.ExchangeConstants;
import com.struggle.common.QueueConstants;
import com.struggle.common.RoutingConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @className TopicRabbitConfig
 * @Description 主题模式消费者配置类
 * @Author liuwj3
 * @Date 2019/10/12 0012 下午 3:21
 **/
@Configuration
public class TopicRabbitConfig {
    public final static String man = "topic.man";
    public final static String woman = "topic.woman";

    @Bean
    TopicExchange getTopicExchange() {
        return new TopicExchange(ExchangeConstants.TOPIC_EXCHANGE);
    }

    @Bean
    public Queue getFirstQueue() {
        return new Queue(QueueConstants.TOPIC_QUEUE_FIRST);
    }

    @Bean
    public Queue getSecondQueue() {
        return new Queue(QueueConstants.TOPIC_QUEUE_SECOND);
    }

    /**
     * firstQueue队列绑定到主题交换机，绑定键是topic.man
     * 只有当消息的路由键是topic.man时才会分发到该队列
     * @return
     */
    @Bean
    public Binding bindingTopicFirst() {
        return BindingBuilder.bind(getFirstQueue()).to(getTopicExchange()).with(RoutingConstants.TOPIC_MAN);
    }

    /**
     * secondQueue队列绑定到主题交换机，绑定键是topic.#
     * 所有路由键以topic.开头的消息都会分发到该队列
     * @return
     */
    @Bean
    public Binding bindingTopicSecond() {
        return BindingBuilder.bind(getSecondQueue()).to(getTopicExchange()).with(RoutingConstants.TOPIC_ALL_PEOPLE);
    }
}
